package com.diandian.backend.dingding.data.service;

import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.dingding.data.domain.DingdingDeptTreeStaff;
import com.diandian.backend.dingding.data.dto.*;
import com.diandian.backend.dingding.data.mapper.DingdingDeptTreeStaffMapper;
import com.diandian.backend.dingding.data.resp.DingdingDeptTreeStaffDetailResp;
import com.diandian.backend.dingding.data.resp.DingdingDeptTreeStaffPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.dingding.data.resp.DingdingDeptTreeStaffListResp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DingdingDeptTreeStaffService {

    @Autowired
    private DingdingDeptTreeStaffMapper dingdingDeptTreeStaffMapper;

    public PageResult page(DingdingDeptTreeStaffPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = dingdingDeptTreeStaffMapper.getCount(requestDTO);

        // 查询数据集合
        List<DingdingDeptTreeStaff> dataList = dingdingDeptTreeStaffMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<DingdingDeptTreeStaffPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                DingdingDeptTreeStaffPageResp item = new DingdingDeptTreeStaffPageResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setUserId(x.getUserId());
                item.setDeptCode(x.getDeptCode());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<DingdingDeptTreeStaffListResp> getList(DingdingDeptTreeStaffListParamDTO requestDTO){

        // 查询数据集合
        List<DingdingDeptTreeStaff> dataList = dingdingDeptTreeStaffMapper.getList(requestDTO);

        // 组装数据
        List<DingdingDeptTreeStaffListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                DingdingDeptTreeStaffListResp item = new DingdingDeptTreeStaffListResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setUserId(x.getUserId());
                item.setDeptCode(x.getDeptCode());
                respList.add(item);
            });
        }

        return respList;
    }

    public DingdingDeptTreeStaffDetailResp getDetail(DingdingDeptTreeStaffDetailParamDTO param) {

        DingdingDeptTreeStaff dbData = dingdingDeptTreeStaffMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        DingdingDeptTreeStaffDetailResp result = new DingdingDeptTreeStaffDetailResp();
        result.setId(dbData.getId());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        result.setUserId(dbData.getUserId());
        result.setDeptCode(dbData.getDeptCode());
        return result;
    }

    public void updateById(DingdingDeptTreeStaffUpdateByIdParamDTO param) {
        param.check();

        DingdingDeptTreeStaffDetailParamDTO queryDetailDTO = new DingdingDeptTreeStaffDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        DingdingDeptTreeStaff dbData = dingdingDeptTreeStaffMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        dingdingDeptTreeStaffMapper.updateById(param);
    }

    public void deleteById(DingdingDeptTreeStaffDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        dingdingDeptTreeStaffMapper.deleteById(paramDTO);
    }

    public void deleteByBelongAndUserIdIn(String belong, List<String> userIdList) {
        dingdingDeptTreeStaffMapper.deleteByBelongAndUserIdIn(belong, userIdList);
    }

    public void add(DingdingDeptTreeStaffAddParamDTO param) {
        dingdingDeptTreeStaffMapper.add(param);
    }

    public void batchAdd(List<DingdingDeptTreeStaffAddParamDTO> dataList) {

        if(dataList == null || dataList.size() == 0){
            return;
        }

        for (DingdingDeptTreeStaffAddParamDTO data : dataList) {
            data.setDel(0);
            data.setCreateTime(new Date());
        }

        dingdingDeptTreeStaffMapper.batchAdd(dataList);
    }
}
